package com.hidtechs.popfax.view;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import com.hidtechs.popfax.controller.Constants;
import com.hidtechs.popfax.controller.Requestor;
import com.hidtechs.popfax.xml.ParsedDeleteDataSet;

public class DeleteContacts extends Activity implements Runnable{
	
	
	Character class_type='e';
	String userName_shared_prefernece;
	String passWord_shared_prefernece;
	protected Handler handler;
	protected HttpURLConnection conn=null;
	protected ArrayList arrayOfObjectOfParsedData;
	public HashMap<String,String> parameterValues;
	private ProgressDialog progressDialog;
	private static String contact_id;
	private static String contactid[][];
	
	public static String getContact_id() {
		return contact_id;
	}


	public static void setContact_id(String contact_id) {
		DeleteContacts.contact_id = contact_id;
	}


	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		deleteContact(getContact_id());
	
	}
	
	
		public void deleteContact(String selected_contact_id)
			{
	
			contact_id=selected_contact_id;
		
			progressDialog = ProgressDialog.show(this,Constants.DIALOG_WAIT_MSG,Constants.FAX_DELETION_MSG, true,false);
			Thread thread = new Thread(this);
			thread.start(); 
			deleteHandlerMethod();

			}
	
		public void run() {
			setMapValues();
			Requestor requestorForXMLParsing= new Requestor();
			arrayOfObjectOfParsedData=requestorForXMLParsing.requestData(class_type,parameterValues);
			handler.sendEmptyMessage(0);		
		}
		
		
		
		public void deleteHandlerMethod()
		{
			handler = new Handler() {

	 		public void handleMessage(Message msg) {
	 			progressDialog.dismiss();

	 			System.out.println("Handler");
	 				 if(((ParsedDeleteDataSet)(arrayOfObjectOfParsedData.get(0))).getResult()==Constants.LOGIN_SUCCESS_CODE)
	 				 {
	 					 Intent tabIntent = new Intent(getApplicationContext(),com.hidtechs.popfax.view.TabWidgetController.class);
	 					 startActivity(tabIntent);
	 				 }
	 				 if(((ParsedDeleteDataSet)(arrayOfObjectOfParsedData.get(0))).getResult()==Constants.LOGIN_FAILURE_CODE)
	     			 {
	     				 Toast.makeText(getApplicationContext(),Constants.LOGGED_IN_FAILED_MSG, Toast.LENGTH_SHORT).show();
	     				 Intent tabIntent = new Intent(getApplicationContext(),com.hidtechs.popfax.view.TabWidgetController.class);
	 					 startActivity(tabIntent);
	     			 }
	 			 
	 		}
	 		
	 	};

		}

		
		
		void setMapValues() {

			loadPreference(); 
			parameterValues = new HashMap();
			parameterValues.put(Constants.URL_ACTION, Constants.DELETE_CONTACT_ACTION);
			parameterValues.put(Constants.URL_USERNAME,userName_shared_prefernece);
			parameterValues.put(Constants.URL_PASSWORD,passWord_shared_prefernece);
			parameterValues.put(Constants.CONTACT_ID,contact_id );
			parameterValues.put(Constants.CONTACT_NUMBER,"" );
		}



		protected void loadPreference()
		{
			int mode = Activity.MODE_PRIVATE;
			SharedPreferences mySharedPreferences = getSharedPreferences("preferences", mode);
			userName_shared_prefernece= mySharedPreferences.getString("USER_NAME","UserName");
			System.out.println(userName_shared_prefernece);
			passWord_shared_prefernece = mySharedPreferences.getString("PASS_WORD","Password");
			System.out.println(passWord_shared_prefernece);
		}
}
